import api from '@/api/index'
import { PAYMENT_TYPE_LIST } from '../constant'

export default {
  state: {
    webViewUrl: '',
    alipay_account: '',
    wechat_account: ''
  },
  mutations: {
    SET_WEBVIEW_URL(state, url) {
      state.webViewUrl = url
    },
    SET_ALIPAY_ACCOUNT(state, account) {
      state.alipay_account = account
    },
    SET_WECHAT_ACCOUNT(state, account) {
      state.wechat_account = account
    }
  },
  actions: {
    /** 设置页面标题 */
    setPageTitle({}, title) {
      uni.setNavigationBarTitle({
        title: title
      })
    },
    getPaymentAccount({commit}) {
      console.log('---', PAYMENT_TYPE_LIST)
      PAYMENT_TYPE_LIST.forEach(item => {
        const key = item.key.toUpperCase()
        // const key = item.key
        api.getConfigInfo({key}).then(res => {
          console.log('获取支付账号', res)
          if (!res) return
          const account = res.data && res.data.value
          const fnKey = key.toUpperCase()
          commit(`SET_${fnKey}`, account)
        })
      })
    },
    /**
     * 设置剪切板
     */
    setClipboard({}, data) {
      return new Promise(resolve => {
        uni.setClipboardData({
          data: data,
          success: () => {
            console.log('设置剪切板成功')
            resolve()
          }
        })
      })
    }
  },
  getters: {}
}